package nuc.zhangchao.test.controller;

import nuc.zhangchao.test.mapper.RoleMapper;
import nuc.zhangchao.test.mapper.UserMapper;
import nuc.zhangchao.test.pojo.Role;
import nuc.zhangchao.test.pojo.User;
import nuc.zhangchao.test.service.RoleService;
import nuc.zhangchao.test.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author Zhang Chao
 * @Date 2020/12/22 14:43
 * @Version 1.0
 */
@Controller
//@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;


    /**
     * 登录操作
     * @param email
     * @param password
     * @param session
     * @param model
     * @return
     */
    @PostMapping("/dologin")
    public String dologin(
            @RequestParam("email") String email,
            @RequestParam("password") String password,
            HttpSession session,
            Model model){
//        System.out.println(email+"     ===========>       "+password);
//        User user = new User(email,password);
        User login = userMapper.login(email,password);
//        System.out.println("这里走了吗"+login);
        if(login!=null){
            session.setAttribute("login",login);
            return "redirect:/index";
        }
        else{
            model.addAttribute("msg","用户名或密码错误！");
            return "login";
        }
//        return "redirect:/login";
    }



    @GetMapping("/toUpdateUser")
    public String toUpdateUser(Model model,@RequestParam("id") Integer id){
        User user =userMapper.selectUserById(id);
        model.addAttribute("user",user);
        return "user_update";
    }
    @PostMapping("/doUpdateUser")
    public String doUpdateUser(User user){
        int i = userMapper.updateUser(user);
        System.out.println("执行结果"+i);
        return "redirect:/userAll";
    }
    //删除操作（简单版）
//    @GetMapping("/delUser")
//    public String delUser(@RequestParam("id") int id){
//        int i = userMapper.delUser(id);
//        System.out.println("删除成功——》"+id);
//        return "redirect:/userAll";
//    }
    //删除操作(多表版)
    @GetMapping("/delUser")
    public String delUser(@RequestParam("id") int id){
        userService.del(id);
        return "redirect:/userAll";
    }




//    @RequestMapping("/list")
//    public String list(){
//        return "/pages/user-list";
////        return "/pages/test";
//    }
//    @RequestMapping("/list")
//    public ModelAndView list(){
//        System.out.println("我来了");
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("pages/user-list");
//        return modelAndView;
//    }


    //用于测试 测试其中的（多表查询）
    @GetMapping("/selectUU")
    public String text(){
        List<User> users_rolelist = userMapper.selectUsers_role();
        System.out.println(users_rolelist);
        return "index_test";
    }
//    //【单表查询  无角色版】
//    @GetMapping("/userAll")
//    public String findUser(Model model){
//        List<User> list=userMapper.selectUsers();
//        model.addAttribute("ulist",list);
//        return "user_list";
//    }

    //【多表查询  有角色版】
    @GetMapping("/userAll")
    public String findUser(Model model){
        List<User> users_rolelist = userMapper.selectUsers_role();
        model.addAttribute("ulist",users_rolelist);
        return "user_list";
    }


    /**
     * 简单版的添加操作
     * @param model
     * @return
     */
    @GetMapping("/toAddUser")
    public String toAddUser(Model model){
        List<Role> roleList = roleMapper.selectRoles();
        model.addAttribute("roleList",roleList);
        return "user_add";
    }
//    @PostMapping("/doAddUser")
//    public String addUser(User user,Long[] roleIds){
//        int i= userMapper.addUser(user);
//        System.out.println("插入成功"+i);
//        return "redirect:/userAll";
//    }

        @PostMapping("/doAddUser")
    public String addUser(User user,int[] roleIds){
        userService.save(user,roleIds);
        System.out.println("插入成功");
        return "redirect:/userAll";
    }


}
